package com.cjm.algorithm.leetcode;

import java.util.Arrays;

public class MinimumDifference1984 {
    public int minimumDifference(int[] nums, int k) {
        if (nums.length == 1) return 0;
        Arrays.sort(nums);
        int left = 0;
        int r = left + k - 1;
        int min = Integer.MAX_VALUE;
        while (r < nums.length) {
            int num = nums[left];
            int num1 = nums[r];
            min = Math.min(min, num1 - num);
            left++;
            r++;
        }
        return min;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{9,4,1,7};
        MinimumDifference1984 minimumDifference1984 = new MinimumDifference1984();
        System.err.println(minimumDifference1984.minimumDifference(nums, 2));
    }
}
